package cn.edu.lcu.cs.javaprogramming.thread;

import java.util.concurrent.TimeUnit;

/**
 * 精灵线程（后台线程）演示
 *
 * @author ling
 * @date 2022/5/30 16:39
 */
public class DaemonThreadDemo {
    public static void main(String[] args) {
        Thread daemon = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName() + " 持续运行！");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }, "精灵线程");

        daemon.setDaemon(true);
        daemon.start();

        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.err.println(Thread.currentThread().getName() + " 要退出了。");
    }
}
